import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

typedef LocaleChangeCallback = void Function(Locale locale);

class I18n implements WidgetsLocalizations {
  const I18n();
  static Locale _locale;
  static bool _shouldReload = false;
  static set locale(Locale newLocale) {
    _shouldReload = true;
    I18n._locale = newLocale;
  }
  static const GeneratedLocalizationsDelegate delegate = GeneratedLocalizationsDelegate();
  static LocaleChangeCallback onLocaleChanged;
  static I18n of(BuildContext context) =>
    Localizations.of<I18n>(context, WidgetsLocalizations);
  @override
  TextDirection get textDirection => TextDirection.ltr;
  String get appName => "Flutter Template";
  
  String get page_setting => "setting";
  String get language => "language";
  String get about => "about";
  String get theme => "Theme";
  String get cache => "cache";
  
  
  String get page_about => "About";
  String get latest => "latest";
  String get update => "update";
  String get version => "version";
  
  String get page_cache => "Cache";
  String get page_theme => "Theme";

  String get page_language => "Language";
  String get chinese => "简体中文";
  String get english => "english";
  String get auto => "auto";
}

class _I18n_en_US extends I18n {
  const _I18n_en_US();
  @override
  TextDirection get textDirection => TextDirection.ltr;
}

class _I18n_zh_CN extends I18n {
  const _I18n_zh_CN();
   String get appName => "Flutter 模板";
   
  String get page_setting => "设置";
  String get language => "语言";
  String get theme => "主题";
  String get about => "关于";
  String get cache => "缓存";
  
  
  String get page_about => "关于";
  String get latest => "最新版";
  String get update => "更新";
  String get version => "版本";
  
  String get page_cache => "缓存";
  String get page_theme => "主题";
  
  String get page_language => "语言";
  String get chinese => "简体中文";
  String get english => "english";
  String get auto => "自动";
  
  @override
  TextDirection get textDirection => TextDirection.ltr;
}

class GeneratedLocalizationsDelegate extends LocalizationsDelegate<WidgetsLocalizations> {
  const GeneratedLocalizationsDelegate();
  List<Locale> get supportedLocales {
    return const <Locale>[
      Locale("en", "US"),
      Locale("zh", "CN")
    ];
  }

  LocaleResolutionCallback resolution({Locale fallback}) {
    return (Locale locale, Iterable<Locale> supported) {
      if (isSupported(locale)) {
        return locale;
      }
      final Locale fallbackLocale = fallback ?? supported.first;
      return fallbackLocale;
    };
  }

  @override
  Future<WidgetsLocalizations> load(Locale locale) {
    I18n._locale ??= locale;
    I18n._shouldReload = false;
    final String lang = I18n._locale != null ? I18n._locale.toString() : "";
    final String languageCode = I18n._locale != null ? I18n._locale.languageCode : "";
    if ("en_US" == lang) {
      return SynchronousFuture<WidgetsLocalizations>(const _I18n_en_US());
    }
    else if ("zh_CN" == lang) {
      return SynchronousFuture<WidgetsLocalizations>(const _I18n_zh_CN());
    }
    else if ("en" == languageCode) {
      return SynchronousFuture<WidgetsLocalizations>(const _I18n_en_US());
    }
    else if ("zh" == languageCode) {
      return SynchronousFuture<WidgetsLocalizations>(const _I18n_zh_CN());
    }

    return SynchronousFuture<WidgetsLocalizations>(const I18n());
  }

  @override
  bool isSupported(Locale locale) {
    for (var i = 0; i < supportedLocales.length && locale != null; i++) {
      final l = supportedLocales[i];
      if (l.languageCode == locale.languageCode) {
        return true;
      }
    }
    return false;
  }

  @override
  bool shouldReload(GeneratedLocalizationsDelegate old) => I18n._shouldReload;
}